package com.icexxx.icemock.mock;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

public class DoubleMock implements IceMock {

	@Override
	public String mock(Integer minVal, Integer maxVal, Integer minWidth, Integer maxWidth, String enums,
			String key) {
		if (maxVal == null) {
			Integer whole = null;
			Integer decimal = null;
			if (minWidth == null && maxWidth == null) {
				whole = 5;
				decimal = 2;
			} else {
				if (minWidth == null) {
					whole = 5;
					decimal = maxWidth;
				} else if (maxWidth == null) {
					whole = minWidth;
					decimal = 2;
				} else {
					whole = minWidth;
					decimal = maxWidth;
				}
			}
			String randomWhole = RandomUtil.randomNumbers(whole);
			if (randomWhole.startsWith("0")) {
				String first = String.valueOf(RandomUtil.randomInt(9) + 1);
				randomWhole = first + randomWhole.substring(1);
			}
			String randomDecimal = RandomUtil.randomNumbers(decimal);
			return randomWhole + "." + randomDecimal;
		} else {
			String whole = null;
			String decimal = null;
			if (minVal == null) {
				minVal = 0;
			}
			int randomInt = RandomUtil.randomInt(minVal, maxVal + 1);
			String str = String.valueOf(randomInt);
			int len = str.length();
			if (minWidth == null) {
				minWidth = 5;
			}
			if (maxWidth == null) {
				maxWidth = 2;
			}
			if (len > minWidth) {
				whole = StrUtil.subPre(str, minWidth);
			} else {
				whole = str;
			}
			decimal = RandomUtil.randomNumbers(maxWidth);
			return whole + "." + decimal;
		}
	}

}
